//
//  RootViewController.m
//  通讯录限时123123
//
//  Created by dllo on 15/12/9.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RootViewController.h"
#import "Student.h"
#import "ChineseToPinyin.h"
#import "AddViewController.h"

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate, AddViewDelegate>

@property (nonatomic, retain) NSMutableArray *stuArr;
@property (nonatomic, retain) NSMutableDictionary *stuDic;
@property (nonatomic, retain) UITableView *tableV;

@end

@implementation RootViewController

- (void)dealloc
{
    [_stuArr release];
    [_stuDic release];
    [_tableV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self getData];
    
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [_tableV release];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftAction:)];
    
    
}

- (void)getStu:(Student *)stu
{
    NSString *key = [[ChineseToPinyin pinyinFromChiniseString:stu.name] substringToIndex:1];
    
    if ([self.stuDic objectForKey:key] != nil) {
        NSMutableArray *arr = [self.stuDic objectForKey:key];
        [arr addObject:stu];
    } else {
        NSMutableArray *arr = [NSMutableArray array];
        [arr addObject:stu];
        [self.stuArr addObject:key];
        [self.stuDic setObject:arr forKey:key];
        [self.stuArr sortUsingSelector:@selector(compare:)];
    }
    
    [self.tableV reloadData];
}

- (void)leftAction:(UIBarButtonItem *)sender
{
    AddViewController *addVC = [[AddViewController alloc] init];
    addVC.delegate = self;
    [self.navigationController pushViewController:addVC animated:YES];
    [addVC release];
    
}

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    } else {
        return sourceIndexPath;
    }
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSString *key = [self.stuArr objectAtIndex:sourceIndexPath.section];
    NSMutableArray *arr = [self.stuDic objectForKey:key];
    Student *stu = [[arr objectAtIndex:sourceIndexPath.row] retain];
    [arr removeObject:stu];
    [arr insertObject:stu atIndex:destinationIndexPath.row];
    [stu release];
    
    [self.tableV reloadData];
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        NSString *key = [self.stuArr objectAtIndex:indexPath.section];
        NSMutableArray *arr = [self.stuDic objectForKey:key];
        if (UITableViewCellEditingStyleDelete == editingStyle) {
            
            
            [arr removeObjectAtIndex:indexPath.row];
            
            if (0 == arr.count) {
                [self.stuDic removeObjectForKey:key];
                [self.stuArr removeObject:key];
            }
            
        }
        
        [self.tableV reloadData];
        
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableV setEditing:editing animated:animated];
}

// youce
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.stuArr;
}

// qubiaoti
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.stuArr objectAtIndex:section];
}


//qu
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.stuArr.count;
}

// hang
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [self.stuArr objectAtIndex:section];
    NSMutableArray *arr = [self.stuDic objectForKey:key];
    return arr.count;
    
}
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *cellStr = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
    }
    NSString *key = [self.stuArr objectAtIndex:indexPath.section];
    NSArray *arr = [self.stuDic objectForKey:key];
    Student *stu = [arr objectAtIndex:indexPath.row];
    cell.textLabel.text = stu.name;
    cell.detailTextLabel.text = stu.phone;
    cell.imageView.image = stu.image;
    
    return cell;
    
}

- (void)getData
{

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HW" ofType:@"plist"];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    
    self.stuDic = [NSMutableDictionary dictionary];
    for (NSString *key in dic) {
        
        NSMutableArray *arr = [NSMutableArray array];
        for (NSMutableDictionary *tempDic in [dic objectForKey:key]) {
            Student *stu = [[Student alloc] init];
            [stu setValuesForKeysWithDictionary:tempDic];
            [arr addObject:stu];
            [stu release];
        }
        [self.stuDic setValue:arr forKey:key];
    }
    self.stuArr = [NSMutableArray arrayWithArray:self.stuDic.allKeys];
    [self.stuArr sortUsingSelector:@selector(compare:)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
